﻿namespace USSDRunner
{
    using System;
    using System.Globalization;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using Microsoft.Phone.Shell;

    using USSDRunner.ViewModels;

    /// <summary>
    /// Represents page for USSD message displaying and editing.
    /// </summary>
    public partial class MessagePage
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MessagePage"/> class.
        /// </summary>
        public MessagePage()
        {
            InitializeComponent();

            ViewModel = App.ViewModel;

            // this.Loaded += HandleLoaded;

            // Re-assign names already in the XAML file
            AddButton = this.ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            RemoveButton = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
            SendButton = this.ApplicationBar.Buttons[2] as ApplicationBarIconButton;
            ClearButton = this.ApplicationBar.Buttons[3] as ApplicationBarIconButton;
        }

        /// <summary>
        /// Gets or sets the view model.
        /// </summary>
        /// <value>
        /// The view model.
        /// </value>
        private MessageViewModel ViewModel
        {
            get
            {
                return (MessageViewModel)DataContext;
            }

            set
            {
                DataContext = value;
            }
        }

        // Load data for the ViewModel Parameters
        // private static void HandleLoaded(object sender, RoutedEventArgs e)
        // {
        //     if (!App.ViewModel.IsDataLoaded)
        //     {
        //         App.ViewModel.LoadData();
        //     }
        // }


        private void HandleAddParameterButtonClick(object sender, EventArgs e)
        {
            ViewModel.AddParameter();
        }

        private void HandleRemoveParameterButtonClick(object sender, EventArgs e)
        {
            ViewModel.RemoveLastParameter();
        }

        private void HandleSendMessageButtonClick(object sender, EventArgs e)
        {
            ViewModel.SendMessage();
        }

        private void HandleClearButtonClick(object sender, EventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Clear this USSD message?", "USSD Runner", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                ViewModel.ClearMessage();
            }
        }

        private void RemoveNonUssdCharacters(TextBox textBox)
        {
            char[] invalidCharacters = { '.', ',', '(', ')', 'x', '-', '+', ' ', '@' };

            int validCharactersBeforeSelectionCount = 0;
            for (int characterIndex = 0; characterIndex < textBox.SelectionStart; characterIndex++)
            {
                if (!invalidCharacters.Contains(textBox.Text[characterIndex]))
                {
                    validCharactersBeforeSelectionCount++;
                }
            }

            bool isTextModified = false;
            foreach (char invalidCharacter in invalidCharacters)
            {
                textBox.Text = textBox.Text.Replace(invalidCharacter.ToString(CultureInfo.InvariantCulture), string.Empty);
                isTextModified = true;
            }

            if (isTextModified)
            {
                textBox.SelectionStart = validCharactersBeforeSelectionCount;
            }
        }

        private void HandleMessageTextBoxKeyUp(object sender, KeyEventArgs e)
        {
            RemoveNonUssdCharacters((TextBox)sender);
        }
    }
}